package org.code.boot.controller.boot;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.code.boot.common.annotation.Log;
import org.code.boot.common.enums.BusinessType;
import org.code.boot.controller.BaseController;
import org.code.boot.vo.AjaxResult;
import org.code.boot.domain.boot.BizTrain;
import org.code.boot.service.boot.IBizTrainService;
import org.code.boot.vo.TableDataInfo;
import org.code.boot.domain.boot.BizTrainSeatConfig;
import org.code.boot.service.boot.IBizTrainSeatConfigService;

/**
 * 火车信息Controller
 * 
 * @author coder
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/boot/train")
public class BizTrainController extends BaseController
{
    @Autowired
    private IBizTrainService bizTrainService;

    @Autowired
    private IBizTrainSeatConfigService bizTrainSeatConfigService;

    /**
     * 查询火车信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizTrain bizTrain, String travelDate)
    {
        startPage();
        
        // 处理出发日期筛选
        if (travelDate != null && !travelDate.isEmpty()) {
            bizTrain.getParams().put("travelDate", travelDate);
        }
        
        List<BizTrain> list = bizTrainService.selectBizTrainList(bizTrain);
        return getDataTable(list);
    }



    /**
     * 获取火车信息下拉选项列表
     */
    @GetMapping("/options")
    public AjaxResult getOptions()
    {
        List<BizTrain> list = bizTrainService.selectBizTrainList(new BizTrain());
        return success(list);
    }

    /**
     * 根据出发站、到达站、日期查询可用火车
     */
    @GetMapping("/search")
    public AjaxResult search(String departureStation, String arrivalStation, String travelDate)
    {
        BizTrain queryTrain = new BizTrain();
        queryTrain.setDepartureStation(departureStation);
        queryTrain.setArrivalStation(arrivalStation);
        List<BizTrain> trainList = bizTrainService.selectBizTrainList(queryTrain);
        return success(trainList);
    }

    /**
     * 获取火车座位配置信息
     */
    @GetMapping("/{trainId}/seats")
    public AjaxResult getSeatConfigs(@PathVariable("trainId") Long trainId)
    {
        List<BizTrainSeatConfig> seatConfigs = bizTrainSeatConfigService.selectBizTrainSeatConfigByTrainId(trainId);
        return success(seatConfigs);
    }

    /**
     * 获取火车信息详细信息
     */
    @GetMapping(value = "/{trainId}")
    public AjaxResult getInfo(@PathVariable("trainId") Long trainId)
    {
        return success(bizTrainService.selectBizTrainByTrainId(trainId));
    }

    /**
     * 新增火车信息
     */
    @Log(title = "火车信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizTrain bizTrain)
    {
        bizTrain.setCreateBy(getNickname());
        return toAjax(bizTrainService.insertBizTrain(bizTrain));
    }

    /**
     * 修改火车信息
     */
    @Log(title = "火车信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTrain bizTrain)
    {
        bizTrain.setUpdateBy(getNickname());
        return toAjax(bizTrainService.updateBizTrain(bizTrain));
    }

    /**
     * 删除火车信息
     */
    @Log(title = "火车信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainIds}")
    public AjaxResult remove(@PathVariable Long[] trainIds)
    {
        return toAjax(bizTrainService.deleteBizTrainByTrainIds(trainIds));
    }
}
